package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;


import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();    // 密码加密对象


    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象

        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User)
                SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if(user != null){
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }

        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }


    //分页查询用户
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return userService.findPage(queryPageBean);
    }


    //添加用户
    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] roleIds){
        try {
            //将用户密码进行加密
            System.out.println(bCryptPasswordEncoder);
            String encodePassword = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(encodePassword);
            userService.add(user,roleIds);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);  //新增用户成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL); //新增用户失败
        }

    }


    //根据用户名查询用户信息
    @RequestMapping("/findById")
    public Result findById(Integer userId){
        try {
            User user = userService.findById(userId);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);    //查询用户成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);   //查询用户失败
        }
    }


    //根据用户id查询关联的角色id
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer userId){
        try {
            Integer[] roleIds = userService.findRoleIdsByUserId(userId);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleIds);     //查询角色成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);   //查询角色失败
        }
    }


    //编辑用户
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds){
        try {
            //将用户密码进行加密
            System.out.println(bCryptPasswordEncoder);
            String encodePassword = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(encodePassword);
            userService.edit(user,roleIds);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);  //编辑用户成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL); //编辑用户失败
        }

    }


    //根据id删除用户
    @RequestMapping("/delete")
    public Result deleteById(Integer id){
        try {
            userService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);    //删除用户成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);      //删除用户失败
        }
    }




}
